﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace GMLogging
{
    public static class Logger
    {
        #region Properties
        private static FileStream mStream;
        private static TextWriterTraceListener mTextWriterTraceListener;
        #endregion

        #region Constructors
        static Logger()
        {
            mStream = null;
            mTextWriterTraceListener = null;
        }
        #endregion

        #region Methods
        public static void Initialize(string pLogFileName)
        {
            try
            {
                mStream = new FileStream(pLogFileName, FileMode.Append, FileAccess.Write);
                mTextWriterTraceListener = new TextWriterTraceListener(mStream);
                Trace.Listeners.Add(mTextWriterTraceListener);
                Trace.AutoFlush = true;
            }
            catch
            {
                Destroy();
            }
        }

        public static void Destroy()
        {
            if (mTextWriterTraceListener != null)
            {
                if (Trace.Listeners.Contains(mTextWriterTraceListener))
                {
                    Trace.Listeners.Remove(mTextWriterTraceListener);
                }
                mTextWriterTraceListener.Dispose();
                mTextWriterTraceListener = null;
            }
            if (mStream != null)
            {
                mStream.Dispose();
                mStream = null;
            }
        }

        #region Info
        public static void Info(string pMessage)
        {
            string fullMessage = GetFullMessage(pMessage);
            Trace.TraceInformation(fullMessage);
        }

        public static void Info(string pFormat, params object[] pArgs)
        {
            string fullMessage = GetFullMessage(pFormat, pArgs);
            Trace.TraceInformation(fullMessage);
        }
        #endregion

        #region Error
        public static void Error(string pMessage)
        {
            string fullMessage = GetFullMessage(pMessage);
            Trace.TraceError(fullMessage);
        }

        public static void Error(string pFormat, params object[] pArgs)
        {
            string fullMessage = GetFullMessage(pFormat, pArgs);
            Trace.TraceError(fullMessage);
        }
        #endregion

        private static string GetFullMessage(string pMessage)
        {
            return GetFormattedDateTime(DateTime.Now) + " : " + pMessage;
        }

        private static string GetFullMessage(string pFormat, params object[] pArgs)
        {
            return GetFormattedDateTime(DateTime.Now) + " : " + string.Format(pFormat, pArgs);
        }

        private static string GetFormattedDateTime(DateTime pDateTime)
        {
            return pDateTime.ToString("yyyy-MM-dd HH:mm:ss.ffff");
        }
        #endregion
    }
}